﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Models;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Net.Mail;
using System.Net;
using System.Data;
namespace OT.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return PartialView();
        }
        private OTEntitiesme OTDb = new OTEntitiesme();
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }

        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }

        
        public string login(string EmailID,string Password,int UserTypeID)
        {
            string result="";
            if (ModelState.IsValid)
            {
                Data.User user = null;
                var mail=Base64Decode(EmailID);
                var pass = Password;
                user = OTDb.Users.Where(r => r.EmailAddress == mail && r.Password ==pass  && r.UserTypeID == UserTypeID).FirstOrDefault();
                if (user == null)
                {
                    result= "Invalid User Name or Password !!";
                }
                else
                {
                    if (user != null && user.UserTypeID == 3)
                    {
                        if (user.Status != true)
                        {
                            result="User Blocked , Please Contact to Admin";
                        }
                        else
                        {

                            HttpCookie myCookie1 = new HttpCookie("LoginSes");
                            myCookie1.Value = user.FirstName;
                            myCookie1.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie1);

                            HttpCookie myCookie2 = new HttpCookie("LoginSesID");
                            myCookie2.Value = user.UserID.ToString();
                            myCookie2.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie2);

                            HttpCookie myCookie3 = new HttpCookie("email");
                            myCookie3.Value = user.EmailAddress;
                            myCookie3.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie3);

                            result = "Student";
                        }
                    }
                    else if (user != null && user.UserTypeID == 2)
                    {
                        if (user.Status != true)
                        {
                            result="User Blocked , Please Contact to Admin";
                        }
                        else
                        {
                            HttpCookie myCookie1 = new HttpCookie("LoginSes");
                            myCookie1.Value = user.FirstName;
                            myCookie1.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie1);

                            HttpCookie myCookie2 = new HttpCookie("LoginSesID");
                            myCookie2.Value = user.UserID.ToString();
                            myCookie2.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie2);

                            HttpCookie myCookie3 = new HttpCookie("email");
                            myCookie3.Value = user.EmailAddress;
                            myCookie3.Expires = DateTime.Now.AddDays(1d);
                            Response.Cookies.Add(myCookie3);

                            result = "Instructor";
                        }
                    }
                }
            }
            return result;
        }

       
    }
}
